﻿using System.IO;
using CtefCrmImport.Common.Enums;

namespace CtefCrmImport.Common.Extensions
{
    public static class StringExtensions
    {
        public static string GenerateFileName(this string input, string extension)
        {
            var name = string.Format("{0}.{1}", input, extension);
            if (!File.Exists(name))
            {
                return name;
            }

            for (var i = 1; ; i++)
            {
                name = string.Format("{0}_{1}.{2}", input, i, extension);
                if (!File.Exists(name))
                {
                    return name;
                }
            }
        }

        public static string ToKey(this string input, bool isInvariantCulture)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return string.Empty;
            }

            return isInvariantCulture ? input.Trim().ToLowerInvariant() : input.Trim().ToLower();
        }

        public static string TrimOrDefault(this string input, string defaultValue)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return defaultValue;
            }

            return input.Trim();
        }

        public static string TrimOrEmpty(this string input)
        {
            return input.TrimOrDefault(string.Empty);
        }

        public static DonationType ToDonationType(this string input)
        {
            if (string.IsNullOrWhiteSpace(input)) return DonationType.Unspecified;

            input = input.Trim().ToLowerInvariant();
            switch (input)
            {
                case "1+1":
                    return DonationType.OnePlusOne;

                default:
                    return DonationType.Unspecified;
            }
        }

        public static PaymentType ToPaymentType(this string input)
        {
            if (string.IsNullOrWhiteSpace(input)) return PaymentType.Unspecified;

            input = input.Trim().ToLowerInvariant();
            switch (input)
            {
                case "cash":
                    return PaymentType.Cash;
                case "check":
                    return PaymentType.Check;
                case "paypal":
                    return PaymentType.PayPal;
                case "creditcard":
                case "credit card":
                    return PaymentType.CreditCard;
                case "payroll":
                    return PaymentType.PayrollDeduction;
                default:
                    return PaymentType.Other;
            }
        }

        public static string RemoveCurrencySymbol(this string input)
        {
            if (string.IsNullOrWhiteSpace(input)) return string.Empty;

            var trimmed = input.Trim();
            var firstChar = trimmed[0];
            if (firstChar < '0' || firstChar > '9')
            {
                return trimmed.Substring(1);
            }

            return trimmed;
        }
    }
}
